<template>
  <el-container class="home-container">
       <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../../assets/logo.png" class="logo" >
        <span class="title">电商后台管理系统</span>
      </div>
      <el-button type='info' @click="logOut()">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse()">
          <i :class="isCollapse?'el-icon-arrow-right':'el-icon-arrow-left'"></i>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                <i class="el-icon-menu"></i>
                {{subItem.authName}}
              </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data() {
    return {
      menulist: [],
      iconsObj: {
        '125': 'el-icon-s-custom',
        '103': 'el-icon-s-cooperation',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-marketing'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logOut() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    getMenuList() {
      // 获取所有的菜单
      this.$http.get('menus').then(res => {
        if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
        this.menulist = res.data.data
      })
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(path) {
      // 保存激活状态
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang='stylus' scoped>
.home-container
  height 100%
  .el-header
    display flex
    justify-content space-between
    align-items center
    padding-left 0
    background-color #373d41
    div
      display flex
      align-items center
      img
        width 60px
      .title
        margin-left 10px
        color #ffffff
        font-size 20px
  .el-aside
    background-color #333744
    .toggle-button
      height 30px
      line-height 30px
      text-align center
      cursor pointer
      background-color #4a5064
      font-size 20px
      color #ffffff
    .el-menu
      border-right 0
  .el-main
    background-color #eaedf1
</style>
